<?php
include_once("database.php");

function saveDates($data) 
{
//$mes1='http://students.info.uaic.ro/~alexandrina.sbiera/colios/panou.php?'.'user='.$data['user'].'&name='.$data['name'].'&email='.$data['email'];
//inlocuiesc spatiile cu un caracter pt a forma bine link-ul.
$us = str_replace(" ","|",$data['user']);
$na = str_replace(" ","|",$data['name']);
$em = str_replace(" ","|",$data['email']);

$mes1='http://colios.romanager.ro/php/panou.php?'.'user='.$us.'&name='.$na.'&email='.$em;
$header="From admin C0li0s:";
@mail('romica_iarca@yahoo.com', 'acceptare', $mes1, $header);

//for ($k = 0; $k<count($data);$k++)
    foreach ($data as $key=>$value) {
        $data[$key] = addslashes($value); //adaugam escape pentru caractere speciale care ar da erori la salvare in baza de date
    }

//verificam daca mai exista un cont cu acelasi nume

    $sqlv  = "SELECT id
		   FROM `users`
		   WHERE `email` = '{$data['email']}'";
    $resv = mysql_query($sqlv) or die("Verificare:".mysql_error());
    if(!mysql_num_rows($resv)) {
        $sql = "INSERT INTO `users`(`name`, `username`, `password`,`email`,`accept`)
			VALUES ('{$data['name']}', '{$data['user']}', '{$data['pasw']}','{$data['email']}',0);";

        $res = mysql_query($sql) or die("eroare la salvare in bd:".mysql_error());
        if (mysql_insert_id())
            return 1;//ok insertt
        else
            return 0;//error insert;
    }
    else
        return -1; //user exists

}
function retrimiteParola($email) {
    $sql  = "SELECT	name
		 FROM users
		 WHERE 	email = '$email'";

    $res = mysql_query($sql) or die("retr:".mysql_error());
  if(mysql_num_rows($res))
    {
 //   $row = mysql_fetch_assoc($res);
    $newpass = generatePass();

    $adresa = $email;
    $mesaj = "Noua dvs parola este:".$newpass;
    $subiect = "Recuperare parola";
    trimite_Mail($email, $subiect, $mesaj);
	  $sql1="UPDATE users
            SET password='$newpass'
            WHERE email='$email'";
     $res1 = mysql_query($sql1) or die("retr:".mysql_error());
   // $row = mysql_fetch_assoc($res1);
    return "Un email a fost trimis la adresa:<b> $adresa</b>";
    }
    else
		return 'Adresa de email nu se gaseste in baza noastra de date.Multumim!';


}

function trimite_Mail($adresaTo, $subiect, $mesaj) {
    $Name = "C0Li0s"; //senders name
    $email = "romica_iarca@yahoo.com"; //senders e-mail adress
    $header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
//    ini_set('sendmail_from', 'suport@wildride.com');

    @mail($adresaTo, $subiect, $mesaj, $header);
}

function generatePass() {
    $allowedChars = "abcdefghijklmnoprstuvxyz1234567890";
    $length = 5;

    $k = 0;
    $pass = '';

    while ($k < $length) {
        $rval = rand(0,strlen($allowedChars));
        //print "<BR>generated :$rval ...trans = ".$allowedChars[$rval];
        //$pass .= $allowedChars[$rval];
        $pass .= substr($allowedChars,$rval,1);
        $k++;
    }

    return $pass;
}

function autentify($data) {


   // $data['user'] = addslashes($data['user']);
   // $data['pasw'] = addslashes($data['password']);
    $sqlv  = "SELECT *
                FROM `users`
		  WHERE	`username` = '{$data['user']}' AND
		   	`password` = '{$data['pasw']}' AND  `accept`=1";
    $resv = mysql_query($sqlv) or die("Logare:".mysql_error());
    if(mysql_num_rows($resv)) {
    //    echo "XXXXX";
        $row = mysql_fetch_assoc($resv);
        //logare cu success...construim datele de sesiune
        $_SESSION['id'] = $row['id'];
        $_SESSION['user'] = $row['username'];
        $_SESSION['name'] = $row['name'];
        $_SESSION['email'] = $row['email'];
        $_SESSION['loggedIn'] = 1;
        $loginok = 1;
    }
    else {
		print '<p> Nu te poti loga.Multumim</p>';
        //stergem sesiunea ...nu ar trebui sa existe
        if(isset($_SESSION['id'])) unset($_SESSION['id']);
        if(isset($_SESSION['user'])) unset($_SESSION['user']);
        if(isset($_SESSION['name'])) unset($_SESSION['name']);
        if(isset($_SESSION['email'])) unset($_SESSION['email']);
        if(isset($_SESSION['loggedIn'])) unset($_SESSION['loggedIn']);
        $loginok = 0;
    }

    return $loginok;
}
function logOutUser()
{
	//stergem sesiunea
		if(isset($_SESSION['id'])) unset($_SESSION['id']);
                if(isset($_SESSION['user'])) unset($_SESSION['user']);
		if(isset($_SESSION['name'])) unset($_SESSION['nume']);
		if(isset($_SESSION['email'])) unset($_SESSION['email']);
		if(isset($_SESSION['loggedIn'])) unset($_SESSION['loggedIn']);
		$loginok = 0;
}
function setAccept($data)
{
	echo "A";
  	//echo "\$param = $param<br />";
	//$data=$param;
	echo "\$data = $data <br />";
	$sqlv="UPDATE users 
		SET accept='1' 
		WHERE email='$data'";
	$resv = mysql_query($sqlv) or die("Logare:".mysql_error());
	
	$subject="Cont C0li0s";
	$message="Ai fost acceptat in baza noastra de date. Multumim.";
	$header="From admin C0li0s:";
	@mail($data, $subject, $message, $header);


//	$row = mysql_fetch_assoc($resv);
}
function reject($data)
{
	
	$sqlv="DELETE FROM users 
		WHERE email='$data'";
	$resv = mysql_query($sqlv) or die("Logare:".mysql_error());
	
	$subject="Cont C0li0s";
	$message="Ne pare rau dar nu indepliniti conditiile necesare pentru a face parte din baza noastra de date. Multumim.";
	$header="From admin C0li0s:";
	@mail($data, $subject, $message, $header);


	//$row = mysql_fetch_assoc($resv);

	
}

function new_pass($data) {
    $sqlv="UPDATE users
          SET password='{$data['pasw2']}'
          WHERE username='{$data['user']}' AND email='{$data['email']}' AND
                password='{$data['pasw']}'";
    $resv=mysql_query($sqlv) or die ("Verifica datele de intrare ".mysql_error());
 //   $row = mysql_fetch_assoc($resv);
    return "Parola a fost schimbata.";
}

function verificare_formular($data)
{
  //print_r($data);
  $error = array("user" => 0, "name" => 0, "passlen" => 0, "pass" => 0, "email" => 0, "ok" => 1);
	if(strlen($data['user']) < 4 || strlen($data['user']) > 20) {
    $error["user"] = 1;
    $error["ok"] = 0;
  }
	if(strlen($data['name']) < 4 || strlen($data['name']) > 20) {
		$error["name"] = 1;
    $error["ok"] = 0;
  }
	if(strlen($data['pasw']) < 4 || strlen($data['pasw']) > 20){
		$error["passlen"] = 1;
    $error["ok"] = 0;
  }
	if($data['pasw'] != $data['pasw2']){
		$error["pass"] = 1;
    $error["ok"] = 0;
	}
  if(!filter_var($data["email"], FILTER_VALIDATE_EMAIL)){
    $error["email"] = 1;
    $error["ok"] = 0;
  }
  return $error;
}
?>
